import { getData } from "@/api/jiugongge/index";
import { timeFilterOptions, countFilterOptions } from "@/data/index";
const isObject = (value) => {
    return value != null && typeof value === 'object'
}

const isInitFilterKey = (key) => {
    return /^init[0-9a-zA-Z]+FilterValue$/.test(key)
}

const lowerFirstLetter = (string) => {
    return string.substring(0, 1).toLowerCase() + string.substring(1)
}

const hasArraAttr = (obj, key) => {
    if (!isObject(obj) || !key) {
        throw new Error('hasArraAttr functional params is illegal!')
    }

    return obj && obj[key] && Array.isArray(obj[key])
}



export const gridviewMixin = {
    props: ['templateName', 'patientBaseId', 'isShow','title'],
    data() {
        return {
            data: [],
            form: {
                // timeFilterValue: ''
            }
        }
    },
    created() {

        // this.initForm()

        this.fetchData()



    },
    computed: {
        patientId() {
            return this.patientBaseId || this.GridContainer.patientBaseId
        },
        headerTitle() {
            if (!this.isValidinput) {
                return ''
            }
            return this.title ?? ''
        },
        currentTemplate() {
            if (this.isValidinput) {
                return ''
                return templateNameMap[this.templateName]
            }
            return null
        },
        isValidinput() { // 有效参数 templateName
            if (!this.templateName) {
                return false
            }
            return true
        },


    },
    watch: {
        patientId() {
     

            this.fetchData()
        },
        currentTemplate() {
  

            this.fetchData()
        }
    },
    methods: {
        async fetchData() {
            if (!this.templateName) {
                throw new Error('templateName must params')
            }
            // if (!templateNames.includes(this.templateName)) {
            //     throw new Error('templateName is illegal')
            // }

            if (!this.patientId) {
                throw new Error('templateName must params')
            }
            const params = {
                templateName: this.templateName,
                data: {
                    ...this.form,
                    patientId: this.patientId
                }
            }
            if(!this.patientId) return
            let res = await getData(params)
            if (res.code === 0) {
                this.data = [...res.data, ...res.data, ...res.data].map(item => {
                    return {
                        ...item,
                        dateTime: item.dateTime.slice(0, 10)
                    }
                })
            }
        },
        handleFilter(va) {
            this.fetchData()
        },
    },
}